﻿using System.Linq;
using FoodRaidersWebAPI.domainnew.other;
using FoodRaidersWebAPI.Models;

namespace FoodRaidersWebAPI.Service.Second
{
    public class FoodService
    {
        private readonly FoodContext context;
        
        public FoodService(FoodContext context)
        {
            this.context = context;
        }
        //id查询 对象不加载
        public Food findByID(int id)
        {
            return context.Foods.FirstOrDefault(f => f.FoodID == id);
        }
        //id查询 全加载
        public Food findByIDFullLoad(int id)
        {
            var food = findByID(id);
            context.Entry(food).Collection(f=>f.LabelObjects).Load();
            return food;
        }
        //删除
        public Food delete(int id)
        {
            var food = findByID(id);
            context.Foods.Remove(food);
            context.SaveChanges();
            return food;
        }
    }
}